#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
const int N = 105, M = 105;
int n, m;
char a[N][N];
bool vis[N][N];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
struct node {
    int x, y;
    int step = 0;
    vector<int> s;
};
bool chk(int x, int y) {
    if (x <= 0 || x > n || y <= 0 || y > m || a[x][y] == '#' || vis[x][y])
        return 0;
    return 1;
}
bool out(int x, int y) {
    return x <= 0 || x > n || y <= 0 || y > m || a[x][y] == '#';
}
int tx = -1, ty = -1, CA;
void go(int x, int y, vector<int> path) {
    for (int i : path) {
        int xx = x + dx[i], yy = y + dy[i];
        if (!out(xx, yy)) {
            a[x][y] = 'A';
            x = xx, y = yy;
            if (a[xx][yy] == 'X') {
                return;
            }
        }
    }
    if (x != tx || y != ty) a[x][y] = 'O';
}
vector<int> ans;

bool E() {
    rep(i, 1, n) rep(j, 1, m) {
        if (a[i][j] == 'O') return 1;
    }
    return 0;
}

int main() {
    scanf("%d%d", &n, &m);
    rep(i, 1, n) scanf("%s", a[i] + 1);
    rep(i, 1, n) rep(j, 1, m) {
        if (a[i][j] == 'X') {
            tx = i, ty = j;
        }
    }
    while (E()) {
        // 1

        queue<node> q;
        rep(i, 1, n) rep(j, 1, m) vis[i][j] = 0;

        q.push({tx, ty});

        node now, tt;
        while (!q.empty()) {
            now = q.front();
            if (a[now.x][now.y] == 'O') tt = now;
            q.pop();
            rep(i, 0, 3) {
                int x = now.x + dx[i];
                int y = now.y + dy[i];

                if (chk(x, y)) {
                    int d = now.step + 1;
                    // vector<int> v = now.s;
                    // v.push_back(i);
                    q.push({x, y, d});
                    vis[x][y] = 1;
                }
            }
        }
        swap(tt, now);
        // 2

        rep(i, 1, n) rep(j, 1, m) vis[i][j] = 0;

        now.s.clear();
        now.step = 0;
        q.push(now);
        while (!q.empty()) {
            now = q.front();
            if (now.x == tx && now.y == ty) break;
            q.pop();
            rep(i, 0, 3) {
                int x = now.x + dx[i];
                int y = now.y + dy[i];
                if (chk(x, y)) {
                    int d = now.step + 1;
                    vector<int> v = now.s;
                    v.push_back(i);
                    q.push({x, y, d, v});
                    vis[x][y] = 1;
                }
            }
        }
        for (int i : now.s) cout << i;
        //  ans.push_back(i);

        // 3
        cout << "\n\n" << ++CA << '\n';

        rep(i, 1, n) rep(j, 1, m) if (a[i][j] == 'O') { go(i, j, now.s); }

        rep(i, 1, n) puts(a[i] + 1);
        if (CA > 5) break;
    }

    // for (int i : ans) cout << i;

    return 0;
}
/*
4 5
O#OOO
OOXOO
OO##O
OOO#O
0011300121133
*/